//
// Created by Administrator on 2021/5/23.
//

#include "protocol.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "define.h"

package_t *alloc_packet(uint32_t data_len) {
    package_t *package = zmalloc(sizeof(package_head_t) + data_len);

    if (package) {
        package->head.length = data_len;
    }

    return package;
}

void free_package(package_t *package) {
    if (package) {
        zfree(package);
    }
}

int packet_decode(buffer_t *buffer, package_t **package) {
    size_t data_len = get_readable_size(buffer);
    if (data_len < sizeof(package_head_t)) {
        return -1;
    }
    package_head_t *package_head = (package_head_t * )(buffer->buff + buffer->read_idx);
    size_t package_len = sizeof(package_head_t) + package_head->length;
    if (data_len < package_len) {
        return -1;
    }

    package_t *new_package = zmalloc(package_len);
    memcpy(new_package, buffer->buff + buffer->read_idx, package_len);
    buffer->read_idx += package_len;

    *package = new_package;

    return 0;
}

int package_encode(buffer_t *buffer, package_t *package) {
    size_t avlid_size = get_writeable_size(buffer);
    size_t package_len = sizeof(package_head_t) + package->head.length;

    if (avlid_size < package_len) {
        return -1;
    }

    memcpy(buffer->buff + buffer->write_idx, package, package_len);
    buffer->write_idx += package_len;

    return 0;
}




